package com.oceansoft.mobile.econsole.modules.message.bizz;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.APIConnectionException;
import cn.jpush.api.common.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

import com.oceansoft.mobile.econsole.modules.message.common.Config;
import com.oceansoft.mobile.econsole.modules.message.entity.MessageContent;
import com.oceansoft.mobile.econsole.modules.message.entity.PushMessage;
import com.oceansoft.mobile.econsole.modules.message.util.Utils;

import org.apache.log4j.Logger;

public class V3Handler {
	Logger logger = Logger.getLogger(V3Handler.class);
	private JPushClient mPushClient;
	private PushMessage message;

	public V3Handler(PushMessage message) {
		mPushClient = new JPushClient(Config.SECRETKEY, Config.APPKEY, 2);
		this.message = message;
	}

	public PushResult push() {
		/*String messageType = message.getReceiver_type(); // 发送类型1广播2群组3别名
		String platform = req.getParameter("platform");// 0android 1ios 2all
		String messageGroup = req.getParameter("messageGroup");// 群组
		String alias = req.getParameter("alias");// 别名
		String title = req.getParameter("title");
		String contentStr = req.getParameter("content");
		String contentType = req.getParameter("contentType");// 消息类型
		String extra = req.getParameter("extra");*/
		PushResult result = new PushResult();

		MessageContent content = message.getMsgContent();
		String platform = message.getPlatform();
		String messageType = String.valueOf(message.getReceiver_type());
		String contentStr = content.getMessage();

		String messageGroup = "";
		String alias = "";
		switch (Integer.valueOf(messageType)) {
			// 发送类型1广播2群组3别名
			case 1:
				break;
			case 2:
				messageGroup = message.getReceiver_value();
				break;
			case 3:
				alias = message.getReceiver_value();
				break;

		}

		PushPayload.Builder mPayloadBuilder = PushPayload.newBuilder();
		// 通用部分
		mPayloadBuilder.setMessage(Message.newBuilder().setTitle(content.getTitle())
				.setMsgContent(contentStr).setContentType(String.valueOf(content.getContent_type())).build());
		try {
			if ((Utils.stringIsNullOrEmpty(messageType))
					|| (Utils.stringIsNullOrEmpty(contentStr))) {
				logger.info("缺少必要参数 content messageType");
				return result;
			}
			if ((Utils.stringIsNullOrEmpty(platform))) {
				platform = "2";
			}

			switch (Integer.valueOf(platform)) {
			case 0:// android
				mPayloadBuilder.setPlatform(Platform.android());
				break;
			case 1:// ios
				mPayloadBuilder.setPlatform(Platform.ios()).setNotification(
						buildIosNotification(contentStr));
				break;
			case 2:
				mPayloadBuilder.setPlatform(Platform.android_ios())
						.setNotification(buildIosNotification(contentStr));
				break;
			}
			// 此处区别发送对象
			mPayloadBuilder.setAudience(buildAudience(messageType,
					messageGroup, alias));

			result = mPushClient.sendPush(mPayloadBuilder.build());
		} catch (NumberFormatException e) {
			// TODO
		} catch (APIConnectionException e) {
			logger.error("Connection error, should retry later", e);
		} catch (APIRequestException e) {
			logger.error("Should review the error, and fix the request", e);
			logger.info("HTTP Status: " + e.getStatus());
			logger.info("Error Code: " + e.getErrorCode());
			logger.info("Error Message: " + e.getErrorMessage());
		}
		return result;
	}

	private Audience buildAudience(String messageType, String messageGroup,
			String alias) {
		Audience mAduience = null;
		switch (Integer.valueOf(messageType)) {
		// 发送类型1广播2群组3别名
		case 1:
			mAduience = Audience.all();
			break;
		case 2:
			mAduience = Audience.tag(messageGroup);

			break;
		case 3:
			mAduience = Audience.alias(alias);
			break;

		}
		return mAduience;
	}

	/**
	 * 构建ios的通知。
	 * @param content
	 * @return
	 */
	private Notification buildIosNotification(String content) {
		return Notification
				.newBuilder()
				.addPlatformNotification(
						IosNotification.newBuilder().setAlert(content)
								.autoBadge().build()).build();

	}

}
